package org.unisinos.gc.action;

import java.io.File;
import java.io.FileInputStream;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.unisinos.gc.vo.Artigo;

public class DownloadFileAction extends Action{
	public ActionForward execute(ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response)
	throws Exception {
		
		String file = request.getParameter("file");
		
		//tell browser we're going to return an application file 
        //instead of html page
		response.setContentType("application/pdf");
		response.setHeader("Content-Disposition","attachment;filename="+file+".pdf");
		
		try 
		{
			
			ServletOutputStream out = response.getOutputStream();
			File fileT = new File(Artigo.DIR+file);
			FileInputStream fileIn = new FileInputStream(fileT);
			byte fileContent[] = new byte[(int)fileT.length()];
			fileIn.read(fileContent);
			
			out.write(fileContent);
			out.flush();
			out.close();
	 
		  }catch(Exception e){
			  System.out.println(e.getMessage());
		  }
		  return null;
		}
}
